package com.tss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.tss.mapper.CertificateMapper;
import com.tss.mapper.CourseMapper;
import com.tss.mapper.StudentMapper;
import com.tss.pojo.Apply;
import com.tss.pojo.Certificate;
import com.tss.pojo.CertificateExample;
import com.tss.pojo.CertificateExample.Criteria;
import com.tss.pojo.CertificateResult;
import com.tss.pojo.Course;
import com.tss.pojo.Result;
import com.tss.pojo.Student;
import com.tss.service.CertificateService;
import com.tss.utils.CertificateUtil;
import com.tss.vo.Dictionary;

@Service
public class CertificateServiceImpl implements CertificateService{
	
	@Autowired
	private CertificateMapper certificateMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private StudentMapper studentMapper;
	
	@Value("#{prop.path}")
	private String path;

	/**
	 * 结课时初始化证书
	 * 在数据库中新增一条数据
	 * 证书文件为空
	 */
	@Override
	public Result initCertificate(Apply apply){
		Result result = new Result();
		
		if(apply==null||apply.getCourseId()==null||apply.getStudentId()==null){
			result.setErrorMessage("参数错误！");
			return result;
		}
		Certificate c = new Certificate();
		c.setValidate(true);
		c.setCourseId(apply.getCourseId());
		c.setStudentId(apply.getStudentId());
		c.setStatus(Dictionary.CERTIFICATE_STATUS_UNISSUED);
		if(findCertificate(c)!=null){
			result.setErrorMessage("证书已经存在！");
			return result;
		}
		certificateMapper.insert(c);
		result.setSuccessMessage("创建证书成功!");
		return result;
		
	}
	
	/**
	 * 生成证书文件
	 */
	@Override
	public Result createCertificate(Certificate certificate) {
		Result result = new Result();
		Certificate c = findCertificate(certificate);
		if(c==null){
			result.setErrorMessage("证书不存在或没初始化！");
			return result;
		}
		Course course = courseMapper.selectByPrimaryKey(c.getCourseId());
		Student student = studentMapper.selectByPrimaryKey(c.getStudentId());
		if(course==null){
			result.setErrorMessage("课程不存在！");
			return result;
		}
		if(student==null){
			result.setErrorMessage("学员不存在！");
			return result;
		}
		String fileName = CertificateUtil.createCertificate(path,student.getRealName(), course.getName(), new Date());
		if(fileName==null){
			result.setErrorMessage("生成证书失败！");
			return result;
		}
		c.setFile(fileName);
		c.setStatus(Dictionary.CERTIFICATE_STATUS_ISSUED);
		certificateMapper.updateByPrimaryKey(c);
		result.setSuccessMessage("创建证书成功!");
		result.addResult("certificate", fileName);
		return result;
	}

	
	@Override
	public Certificate findCertificate(Certificate certificate) {
		if(certificate==null){
			return null;
		}
		if(certificate.getId()!=null){
			return certificateMapper.selectByPrimaryKey(certificate.getId());
		}
		CertificateExample example = new CertificateExample();
		Criteria criteria = example.createCriteria();
		if(certificate.getCourseId()!=null){
			criteria.andCourseIdEqualTo(certificate.getCourseId());
		}
		if(certificate.getStudentId()!=null){
			criteria.andStudentIdEqualTo(certificate.getStudentId());
		}
		criteria.andValidateEqualTo(true);
		List<Certificate> list = certificateMapper.selectByExample(example);
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
	
	}

	
	
	@Override
	public List<CertificateResult> listCertificate(Certificate certificate) {
		CertificateExample example = new CertificateExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		if(certificate!=null){
			if(certificate.getCourseId()!=null){
				criteria.andCourseIdEqualTo(certificate.getCourseId());
			}
			if(certificate.getStudentId()!=null){
				criteria.andStudentIdEqualTo(certificate.getStudentId());
			}
			if(certificate.getStatus()!=null){
				criteria.andStatusEqualTo(certificate.getStatus());
			}
		}
		List<Certificate> list= certificateMapper.selectByExample(example);
		if(list.isEmpty()){
			return null;
		}
		List<CertificateResult> resultList = new ArrayList<CertificateResult>();
		for(Certificate c : list){
			Student student = studentMapper.selectByPrimaryKey(c.getStudentId());
			Course course = courseMapper.selectByPrimaryKey(c.getCourseId());
			if(student==null||course==null){
				continue;
			}
			CertificateResult certificateResult = new CertificateResult();
			certificateResult.setId(c.getId());
			certificateResult.setFile(c.getFile());
			certificateResult.setStudentName(student.getRealName());
			certificateResult.setCoursepName(course.getName());
			if(c.getStatus()==Dictionary.CERTIFICATE_STATUS_ISSUED){
				certificateResult.setStatus("ISSUED");
			}
			if(c.getStatus()==Dictionary.CERTIFICATE_STATUS_UNISSUED){
				certificateResult.setStatus("UNISSUED");
			}
			resultList.add(certificateResult);
		}
		return resultList;
	}

	public void setPath(String path) {
		this.path = path;
	}



	
	

}
